﻿
namespace System.Windows.Forms
{
    public static class TextBoxExtensions
    {
        public static void TabKeyHandler(this TextBox textBox, int tabSize)
        {
            int position = textBox.SelectionStart;
            var tabString = new string(' ', tabSize);

            var currentText = textBox.Text;
            if (position == textBox.TextLength)
                textBox.Text = currentText + tabString;
            else
                textBox.Text =
                    currentText.Substring(0, position) +
                    tabString +
                    currentText.Substring(position);
            textBox.SelectionStart = (position + tabSize);
        }

        public static void TabSizeChanged(this TextBox textBox, int oldTabSize, int newTabSize)
        {
            textBox.Text = textBox.Text.Indent(oldTabSize, newTabSize);
        }
    }
}
